package primitives;

import javax.vecmath.*;

import essential.Axis;

public class Transform implements Transformable {
	Vector3d origin = new Vector3d(0, 0, 0);
	Matrix4d matrix = new Matrix4d();
	private Matrix4d tmpMat = new Matrix4d();
	
	public Transform() {
		matrix = new Matrix4d();
		matrix.setIdentity();
	}
	
	@Override
	public void rotate(Axis axis, double angles) {
		tmpMat.setIdentity();

		switch(axis) {
		case Y:
			tmpMat.rotY(degreeToRad(angles));
			break;
		case X:
			tmpMat.rotX(degreeToRad(angles));
			break;
		case Z:
			tmpMat.rotZ(degreeToRad(angles));
			break;
		}

 		matrix.mul(tmpMat, matrix);
	}
	
	public double degreeToRad(double degree){
		return -degree*Math.PI/180;
	}

	@Override
	public void scale(Vector3d v) {
		tmpMat.setIdentity();
		tmpMat.m00 = v.x;
		tmpMat.m11 = v.y;
		tmpMat.m22 = v.z;
		matrix.mul(tmpMat, matrix);
	}

	@Override
	public void translate(Vector3d pos) {
		tmpMat.setIdentity();
		tmpMat.setTranslation(pos);
		matrix.mul(tmpMat, matrix);
	}

	@Override
	public Vector3d getOrigin() {
		return origin;
	}

	@Override
	public void freezeTransform() {}

	@Override
	public void apply(Transform t) {
		matrix.mul(t.matrix, matrix);
	}

	@Override
	public void set(Transform t) {
		matrix.setIdentity();
		matrix.mul(t.matrix, matrix);
	}
}